package com.blog.backend.service.impl;

import com.blog.backend.config.RedisValueConfig;
import com.blog.backend.domain.BlogAccess;
import com.blog.backend.domain.BlogPerson;
import com.blog.backend.domain.BlogPersonWithBLOBs;
import com.blog.backend.mapper.*;
import com.blog.backend.req.AssessReq;
import com.blog.backend.resp.BlogAssessResp;
import com.blog.backend.resp.PersonResp;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.service.SysSettingService;
import com.blog.backend.utils.RedisUtils;
import com.blog.backend.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysSettingServiceImpl implements SysSettingService {

    @Resource
    private SysSettingMapper sysSettingMapper;

    @Resource
    private BlogPersonMapper blogPersonMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private BlogArticleMapper blogArticleMapper;

    @Resource
    private FoodMapper foodMapper;

    @Resource
    private TravelManagerMapper travelManagerMapper;

    @Resource
    private BlogAccessMapper blogAccessMapper;

    @Override
    public String getEmail() {
        String email=sysSettingMapper.getEmail();
        return email;
    }

    @Override
    public String getAdvertisementUrl() {
       String url= sysSettingMapper.getAdvertisementUrl();
        return url;
    }

    @Override
    public BlogPersonWithBLOBs getPersonInfo() {
        BlogPersonWithBLOBs personWithBLOBs = blogPersonMapper.selectByPrimaryKey();
        //查寻头像
        PictureUrlResp pictureUrlResp = imageMapper.selectImage(personWithBLOBs.getProfile());
        personWithBLOBs.setPicture(pictureUrlResp);
        return personWithBLOBs;
    }

    @Override
    public BlogPerson getPersonSimpleInfo() {
        BlogPerson person= blogPersonMapper.selectSimpleInfo();
        PictureUrlResp pictureUrlResp = imageMapper.selectImage(person.getProfile());
        person.setPicture(pictureUrlResp);
        return person;
    }

    @Override
    public Boolean addBrowseNum(String articleId, Integer type) {
        if (type==1){
            blogArticleMapper.updateBrowseNumById(articleId);
        }else if (type==2){
            foodMapper.updateBrowseNumById(articleId);
        }else {
            travelManagerMapper.updateBrowseNumById(articleId);
        }
        return true;
    }

    @Override
    public Boolean addLikeNum(String articleId, Integer type) {
        if (type==1){
            blogArticleMapper.addLikeNum(articleId);
        }else if (type==2){
            foodMapper.addLikeNum(articleId);
        }else {
            travelManagerMapper.addLikeNum(articleId);
        }
        return true;
    }

    @Override
    public Boolean addComment(AssessReq req) {
        if ( req!=null && StringUtils.isNotBlank(req.getComment())){
            req.setAssessId(UuidUtils.getId());
            if (StringUtils.isEmpty(req.getUserName())){
                req.setUserName("游客"+UuidUtils.getId());
            }
        }
        BlogAccess blogAccess = new BlogAccess();
        blogAccess.setAssessId(req.getAssessId());
        blogAccess.setAssessContent(req.getComment());
        blogAccess.setAssessUser(req.getUserName());
        blogAccess.setArticleId(req.getArticleId());
        blogAccess.setContentType(req.getArticleType());
        blogAccessMapper.insertSelective(blogAccess);
        return true;
    }

    @Override
    public List<BlogAssessResp> getArticleAssess(String articleId) {
        List<BlogAssessResp> list= blogAccessMapper.getArticleAssessByArticleId(articleId);
        return list;
    }
}
